package com.efast.dmom.mgmt.service.les.impl;

import com.efast.dmom.mgmt.bean.les.InventoryQuantityBean;
import com.efast.dmom.mgmt.bean.les.TwInvPalMriJobBean;
import com.efast.dmom.mgmt.bean.les.WindowReplenishmentBean;
import com.efast.dmom.mgmt.entity.les.TwInvPacktranDetail;
import com.efast.dmom.mgmt.entity.les.TwInvPacktranHeader;
import com.efast.dmom.mgmt.entity.les.TwInvPalMri;
import com.efast.dmom.mgmt.service.les.TwBasDpathWindowTimeService;
import com.efast.dmom.mgmt.service.les.TwInvPacktranDetailService;
import com.efast.dmom.mgmt.service.les.TwInvPacktranHeaderService;
import com.efast.dmom.mgmt.service.les.TwInvPalMriService;
import com.efast.dmom.mgmt.service.les.WindowReplenishmentByInventoryConsumptionJobService;

import com.efast.dmom.mgmt.utils.MesConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 【窗口补货-按库存消耗】 窗口补货 ：Window replenishment 库存消耗： Inventory consumption
 *
 * 1：零件仓库关系里面定义了库位是【补货类型】是 窗口补货-按库存消耗
 * 
 * @author Administrator
 */
@Service
public class WindowReplenishmentByInventoryConsumptionJobServiceImpl
		implements WindowReplenishmentByInventoryConsumptionJobService {

	/**
	 * 补货任务需求 TW_INV_PAL_MRI
	 */
	@Autowired
	private TwInvPalMriService twInvPalMriService;
	/**
	 * 实际窗口时间 TW_BAS_DPATH_WINDOW_TIME
	 */
	@Autowired
	private TwBasDpathWindowTimeService twBasDpathWindowTimeService;
	/**
	 * 补货任务头表 TW_INV_PACKTRAN_HEADER
	 */
	@Autowired
	private TwInvPacktranHeaderService twInvPacktranHeaderService;
	/**
	 * 补货任务明细TW_INV_PACKTRAN_DETAIL
	 */
	@Autowired
	private TwInvPacktranDetailService twInvPacktranDetailService;

	@Override
	public synchronized void excute() {

		// 【1】 Job执行的时候：如果当前时间>=窗口时间，按照路径分组找出符合条件的补货路径，查看是否有补货需求。
		List<WindowReplenishmentBean> list = twBasDpathWindowTimeService.queryWindowReplenishment();
		if (list.size() == 0) {
			System.out.println("没有满足的窗口时间........");
		}
		// 【2】 遍历每一条符合时间的补货路径
		for (WindowReplenishmentBean windowReplenishmentBean : list) {

			// 【3】 找出该补货路径下面所有的库位的零件消耗情况
			List<InventoryQuantityBean> list2 = twBasDpathWindowTimeService.queryInventoryQuantityByDpathId(
					windowReplenishmentBean.getTmBasDpathId());

			// 【4】 遍历每个库位的零件消耗情况，判断是否需要产生补货需求
			for (int j = 0; j < list2.size(); j++) {
				InventoryQuantityBean inventoryQuantityBean = list2.get(j);

				// 如果当前库存总数<min值，组织补货需求，这里分两种情况：
				if (inventoryQuantityBean.getCurQty() < inventoryQuantityBean.getMinStorage()) {

					// 【5】查询该库位零件，前面未完成的补货数量
					TwInvPalMriJobBean twInvPalMriJobBean = twBasDpathWindowTimeService.queryNotQuantityOfReplenishment(
							inventoryQuantityBean.getAfterPartLoc(), inventoryQuantityBean.getSupplNo(),
							inventoryQuantityBean.getPartNoVersion());
					Integer notReplenishmentCount = twInvPalMriJobBean.getNotReplenishmentCount();
					// 需要补货数量
					Integer needReplenishmentCount;
					// 需要补货容量
					Integer needReplenishmentCountNum;
					// 如果该库位入库包装容量为null，则当次补货数量=Max-当前库存数-前面未完成的补货数量(要求补货数量-实际补货数量)
					if (inventoryQuantityBean.getInboundPackQty() == null) {
						// 可能是null，则表面产生的补货需求从来都没有人去补货过
						if (notReplenishmentCount == null) {
							needReplenishmentCount = inventoryQuantityBean.getMaxStorage()
									- inventoryQuantityBean.getCurQty();
						} else {
							needReplenishmentCount = inventoryQuantityBean.getMaxStorage()
									- inventoryQuantityBean.getCurQty() - notReplenishmentCount;
							if (needReplenishmentCount <= 0) {
								System.out.println("前面未完成的补货数量"+notReplenishmentCount);
								System.out.println("此次job需要补货数量"+needReplenishmentCount+"。不产生补货需求和补货任务");
								return;
							}
							if (notReplenishmentCount>=needReplenishmentCount) {
								System.out.println("需要补货数量"+needReplenishmentCount);
								System.out.println(inventoryQuantityBean.getPartNoVersion()+"前面未完成的补货数量>=此次需要补货的数量，不产生补货任务");
								return;
							}
						}
					} else {
						// 如果入库包装容量不为null，则补货数量=补货包装数量*入库包装容量-前面未完成的补货数量
						if (notReplenishmentCount == null) {
							needReplenishmentCount = inventoryQuantityBean.getInboundPackQty()
									* inventoryQuantityBean.getInboundPackQty();
						} else {
							needReplenishmentCount = inventoryQuantityBean.getInboundPackQty()
									* inventoryQuantityBean.getInboundPackQty() - notReplenishmentCount;
							if (needReplenishmentCount <= 0) {
								System.out.println("前面未完成的补货数量"+notReplenishmentCount);
								System.out.println("此次job需要补货数量"+needReplenishmentCount+"。不产生补货需求和补货任务");
								return;
							}
							if (notReplenishmentCount>=needReplenishmentCount) {
								System.out.println("需要补货数量"+needReplenishmentCount);
								System.out.println("前面未完成的补货数量>=此次需要补货的数量，不产生补货任务");
								return;
							}
						}
					}
					needReplenishmentCountNum = needReplenishmentCount / inventoryQuantityBean.getInboundPackQty();

					// 【6】：生成 补货任务头表 TW_INV_PACKTRAN_HEADER
					TwInvPacktranHeader twInvPacktranHeader = new TwInvPacktranHeader();
					// 任务状态（新任务0，任务确认1，任务完成2，任务部分完成3，关闭4）
					twInvPacktranHeader.setPacktranStatus("0");
					// 补货任务单打印状态（0打印 1已打印）
					twInvPacktranHeader.setPrintStatus("0");
					// 补货类型（1即时补货 2按库存消耗补货 3按累计消耗补货）
					twInvPacktranHeader.setPacktranType("2");
					// 补货路径
					twInvPacktranHeader.setRebRoad(windowReplenishmentBean.getDpathNo());
					// 目的地
					twInvPacktranHeader.setDeliveryRec(windowReplenishmentBean.getDeliveryRec());
					twInvPacktranHeader.setMarkStatus(1);
					twInvPacktranHeader.setCreateTime(new Date());
					twInvPacktranHeader.setTaskCreatetime(new Date());
					Map<String, Object> map = twInvPacktranHeaderService.insertNonEmptyTwInvPacktranHeader(
							twInvPacktranHeader);

					// 【7】：补货任务明细TW_INV_PACKTRAN_DETAIL
					TwInvPacktranDetail twInvPacktranDetail = new TwInvPacktranDetail();
					BeanUtils.copyProperties(inventoryQuantityBean, twInvPacktranDetail);
					twInvPacktranDetail.setTwInvPacktranHeaderId((int) map.get("twInvPacktranHeaderId"));
					// 零件号
					twInvPacktranDetail.setPartNoVersion(inventoryQuantityBean.getPartNoVersion());
					// 供应商编号
					twInvPacktranDetail.setSupplNo(inventoryQuantityBean.getSupplNo());
					// 零件中文名称
					twInvPacktranDetail.setPartNamec(inventoryQuantityBean.getPartNamec());
					// 零件基本单位
					twInvPacktranDetail.setBaseUnit(inventoryQuantityBean.getBaseUnit());
					// 物料目标仓库
					twInvPacktranDetail.setAfterPartWarehouse(inventoryQuantityBean.getAfterPartWarehouse());
					// 物料目标库区
					twInvPacktranDetail.setAfterPartDloc(inventoryQuantityBean.getAfterPartDloc());
					// 物料目标库位
					twInvPacktranDetail.setAfterPartLoc(inventoryQuantityBean.getAfterPartDloc());
					// 源仓库
					twInvPacktranDetail.setPFromWarehouseId(inventoryQuantityBean.getpFromWarehouseId());
					// 源库区
					twInvPacktranDetail.setPFromDlocId(inventoryQuantityBean.getpFromDlocId());
					// 源库位
					twInvPacktranDetail.setPFromLocId(inventoryQuantityBean.getpFromLocId());
					// 要求补货数量
					twInvPacktranDetail.setPrePacktranQty(needReplenishmentCount);
					// 要求补货包装数量
					twInvPacktranDetail.setPacktranPacknum(needReplenishmentCountNum);
					// 要求补货包装容量
					twInvPacktranDetail.setPacktranPackqty(inventoryQuantityBean.getInboundPackQty());
					// 补货类型（1即时补货 2按库存消耗补货 3按累计消耗补货）
					twInvPacktranDetail.setPacktranType("2");
					// 创建类型（自动/人工）
					twInvPacktranDetail.setTaskCreateType("0");
					// 补货任务行状态
					twInvPacktranDetail.setTaskStatue("0");
					// 指令创建时间
					twInvPacktranDetail.setTaskCreateTime(new Date());
					// 目的地
					twInvPacktranDetail.setDeliveryRec(windowReplenishmentBean.getDeliveryRec());
					// 补货任务单号
					twInvPacktranDetail.setPacktranSheetNo((String) map.get("packtranSheetNo"));
					// 行号
					twInvPacktranDetail.setLineNo(j + 1);
					// 是否需要分配库存(0:无来源库位需要分配|1：有来源库位，不需要分配)
					twInvPacktranDetail.setAcallFlag("1");
					twInvPacktranDetail.setMarkStatus(1);
					twInvPacktranDetail.setCreateTime(new Date());
					twInvPacktranDetailService.insertTwInvPacktranDetail(twInvPacktranDetail);

					// 【8】产生补货需求
					TwInvPalMri twInvPalMri = new TwInvPalMri();
					// 实际补货数量
					twInvPalMri.setActRepQty(0);
					// 要求补货数量
					twInvPalMri.setPrePacktranQty(needReplenishmentCount);
					// // 要求补货包装数量(箱数)
					twInvPalMri.setPacktranPacknum(needReplenishmentCountNum);
					// 补货任务单id
					twInvPalMri.setTwInvPacktranHeaderId((int) map.get("twInvPacktranHeaderId"));
					// 补货任务单明细id
					twInvPalMri.setTwInvPacktranDetailId(twInvPacktranDetail.getTwInvPacktranDetailId());
					// 补货任务单号
					twInvPalMri.setPacktranSheetNo((String) map.get("packtranSheetNo"));
					// 补货任务类型(1:即时补货;2:按库存消耗补货 3按累计消耗补货)
					twInvPalMri.setPacktranType("2");
					// 零件号
					twInvPalMri.setPartNoVersion(inventoryQuantityBean.getPartNoVersion());
					// 供应商编号
					twInvPalMri.setSupplNo(inventoryQuantityBean.getSupplNo());
					// 零件中文名称
					twInvPalMri.setPartNamec(inventoryQuantityBean.getPartNamec());
					// 零件基本单位
					twInvPalMri.setBaseUnit(inventoryQuantityBean.getBaseUnit());
					// 补货任务目标仓库
					twInvPalMri.setAfterPartWarehouse(inventoryQuantityBean.getAfterPartWarehouse());
					// 补货任务目标 库区
					twInvPalMri.setAfterPartDloc(inventoryQuantityBean.getAfterPartDloc());
					// 补货任务目标库位
					twInvPalMri.setAfterPartLoc(inventoryQuantityBean.getAfterPartLoc());
					// 补货任务来源仓库
					twInvPalMri.setpFromWarehouseId(inventoryQuantityBean.getpFromWarehouseId());
					// 补货任务来源库区
					twInvPalMri.setpFromDlocId(inventoryQuantityBean.getpFromDlocId());
					// 补货任务来源库位
					twInvPalMri.setpFromLocId(inventoryQuantityBean.getpFromLocId());
					// 要求补货包装代码
					twInvPalMri.setPacktranPackno(inventoryQuantityBean.getPacktranPackno());
					// 要求补货包装容量
					twInvPalMri.setPacktranPackqty(inventoryQuantityBean.getInboundPackQty());
					// 补货任务创建时间
					twInvPalMri.setPacktranCreatetime(new Date());
					// 补货任务需求状态(0:未组织;1已组织2:补货任务完成3:关闭)
					twInvPalMri.setMriStatue("1");
					// 货主 默认''qoros'' 【待确认】
					twInvPalMri.setCustomerid(MesConstant.DELIVERY_UNIT_NO);
					// 收货地
					twInvPalMri.setDeliveryRec(inventoryQuantityBean.getDeliveryRec());
					// 补货路径
					twInvPalMri.setDpathNo(inventoryQuantityBean.getDpathNo());
					// 是否翻dolley
					twInvPalMri.setDolleyFlag(inventoryQuantityBean.getDolleyFlag());
					// 是否激活
					twInvPalMri.setMarkStatus(1);
					twInvPalMriService.insertTwInvPalMri(twInvPalMri);
				} else {
					System.out.println();
					System.err.println("窗口补货-按库存消耗，此次定时任务零件号：" + inventoryQuantityBean.getPartNoVersion()
							+ "，没有产生补货任务............");
					System.out.println();
				}
			}
		}
	}
}
